import {
  config,
  baseApiUrl
} from '../../config/cfg';
import {
  couponList,
  myCouponList
} from '../../model/coupon/coupon';
import {
  getRequest,
  postRequest
} from '../../utils/request';
const {
  delay
} = require('../../utils/delay');
const app = getApp();


function mockGetCoupons() {
  return delay().then(() => {
    return couponList;
  });
}

function mockGetCouponById(couponId) {
  return delay().then(() => {
    let coupon = couponList.filter(item => item.couponId === couponId);
    return coupon[0];
  });
}

function realGetCouponById(couponId) {
  // var params = {url:baseApiUrl+'/真实子级地址?couponId='+couponId};
  var params = {
    url: app.get_request_url("couponhead", "coupons"),
    data: {
      couponId: couponId
    }
  };
  console.log(params)
  return postRequest(params);
}

function realGetCouponByCode(couponCode) {
  // var params = {url:baseApiUrl+'/真实子级地址?couponId='+couponId};
  var params = {
    url: app.get_request_url("coupondetail", "coupons"),
    data: {
      couponCode: couponCode
    }
  };
  console.log(params)
  return postRequest(params);
}

function realGetCoupons(mobile) {
  //  var params = {url:baseApiUrl+'/真实子级地址'};
  var params = {
    url: app.get_request_url("getcouponlist", "coupons"),
    data: {
      mobile: mobile
    }
  };
  return getRequest(params);
}

function realGetCouponsNow(mobile) {
  //  var params = {url:baseApiUrl+'/真实子级地址'};
  var params = {
    url: app.get_request_url("CouponDetailRefresh", "coupons"),
    data: {
      mobile: mobile
    }
  };
  return getRequest(params);
}

export const getCouponsNow = (mobile) => {
  //  if (config.useMock) {
  //    return mockGetCoupons();
  //  }else{
  return realGetCouponsNow(mobile);
  //  }
}
export const getCoupons = (mobile) => {
  //  if (config.useMock) {
  //    return mockGetCoupons();
  //  }else{
  return realGetCoupons(mobile);
  //  }
}

export const getCouponById = (couponId) => {
  // if (config.useMock) {
  //   return mockGetCouponById(couponId);
  // }else{
  return realGetCouponById(couponId);
  // }
}

export const getCouponByCode = (couponCode) => {
  return realGetCouponByCode(couponCode);
}
export const getCouponByCodeNow = (couponCode) => {
  return realGetCouponByCode(couponCode);
}


export const getMyCoupons = (mobile) => {
  // if (config.useMock) {
  //   return mockGetMyCoupons(mobile);
  // }else{
  return realGetMyCoupons(mobile);
  // }
}

function mockGetMyCoupons(mobile) {
  return delay().then(() => {
    return myCouponList;
  });
}

function realGetMyCoupons(mobile) {
  // var params = {url:baseApiUrl+'/真实子级地址?mobile='+mobile};
  var params = {
    url: app.get_request_url("getmycouponlist", "coupons"),
    data: {
      mobile: mobile
    }
  };
  return postRequest(params);
}

function myCouponsList(mobile) {
  // var params = {url:baseApiUrl+'/真实子级地址?mobile='+mobile};
  var params = {
    url: app.get_request_url("giveCoupon", "coupons"),
    data: {
      mobile: mobile
    }
  };
  return postRequest(params);
}
export const get_MyCouponsList = (mobile) => {

  return myCouponsList(mobile);
}